package com.yeebo.utils;

import com.github.pagehelper.Page;
import com.yeebo.enums.ResultCode;
import com.yeebo.vo.Result;

import java.util.HashMap;
import java.util.Map;

/**
 * 返回封装工具类
 */
public class ResponseUtils {

    public static Result success(Object data) {
        if (data instanceof Page) {
            Map<String, Object> body = new HashMap<>();
            body.put("currentPageNum", ((Page) data).getPageNum());
            body.put("totalPageCount", ((Page) data).getPageNum());
            body.put("totalCount", ((Page) data).getTotal());
            body.put("list", ((Page) data).getResult());

            return new Result(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), body);
        }
        return new Result(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
    }

    public static Result error(int code, Object data) {
        return new Result(code, ResultCode.getMsgByCode(code), data);
    }

    public static Result error(Object data) {
        return error(ResultCode.ERROR.getCode(), data);
    }

}
